package com.suneast.suneaststore.modules.homepage.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.suneast.suneaststore.R;
import com.suneast.suneaststore.bean.homepage.FilterBean;

import java.util.List;

public class SubCateListAdapter extends RecyclerView.Adapter<SubCateListAdapter.VH> {

    private List<FilterBean> dataList;

    private int curPos;

    public SubCateListAdapter(List<FilterBean> dataList) {
        this.dataList = dataList;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SubCateListAdapter.VH(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_home_page_filter_menu, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        holder.tv.setText(dataList.get(position).getName());
        holder.tv.setTextColor(holder.itemView.getResources()
                .getColor(curPos == position ? R.color.red : R.color.black));
        holder.tv.setOnClickListener((view) -> {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(dataList.get(position).getId(), position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    public static class VH extends RecyclerView.ViewHolder {
        public TextView tv;

        public VH(View view) {
            super(view);
            tv = itemView.findViewById(R.id.tv_name);
        }
    }

    public void updatePos(int pos) {
        curPos = pos;
        notifyDataSetChanged();
    }

    public void updateDatas(List<FilterBean> dataList) {
        this.dataList.clear();
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    private OnItemClickListener itemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int id, int pos);
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}
